<%@ page import="org.f0rb.core.web.WebModel" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
 Usage:
<jsp:include page="/WEB-INF/jsp/common/pagebar.jsp">
    <jsp:param name="limit" value="<%=limit%>"/>
    <jsp:param name="offset" value="<%=offset%>"/>
    <jsp:param name="total" value="<%=total%>"/>
</jsp:include>
--%>
<%@ page pageEncoding="UTF-8" %>
<%
    Integer limit = 0, offset;
    Long total;
    WebModel model = (WebModel) request.getAttribute("model");
    if (model != null) {
        limit = model.getLimit();
        offset = model.getOffset();
        total = model.getTotal();
    } else {
        try {
            limit = Integer.valueOf(request.getParameter("limit"));
        } catch (NumberFormatException e) {
        } finally {
            if (limit == 0) limit = 20;
        }
        try {
            offset = Integer.valueOf(request.getParameter("offset"));
        } catch (NumberFormatException e) {
            offset = 0;
        }
        try {
            total = Long.valueOf(request.getParameter("total"));
        } catch (NumberFormatException e) {
            total = 0L;
        }
    }
    Integer p = offset / limit + 1;//current page
    Integer pages = (int) Math.ceil((float) total / limit);
%>
<jsp:include page="/WEB-INF/jsp/head/jquery.jsp"/>
<script type="text/javascript" src="<c:url value="/resources/lib/jquery/jquery.query.js"/>"></script>
<script type="text/javascript">
    window.page = {
        first:function () {
            location.href = $.query.set('offset', 0).toString();
        },
        last:function () {
            location.href = $.query.set('offset', <%=total - total % limit%>).toString();
        },
        prev:function () {
            location.href = $.query.set('offset', <%=(offset > limit) ? offset - limit : 0%>).toString();
        },
        next:function () {
            location.href = $.query.set('offset', <%=offset + limit%>).toString();
        },
        size:function (size) { // 设置每页显示条数
            location.href = $.query.set('limit', size).set('offset', <%=offset%> - <%=offset%> % size).toString();
        },
        goto:function (p) {
            location.href = $.query.set('offset', (p - 1) * <%=limit%>).toString();
        }
    };
</script>
<div>
    <span>
        <label>每页<select onchange="page.size(this.value);">
            <%=(0 < limit && limit < 10) ? "<option selected>" + limit + "</option>" : "" %>
            <% for (int i = 10; i <= 50; i += 10) {%>
            <option <%=(i == limit) ? "selected" : ""%>><%=i%></option>
            <% } %>
            <%=(limit > 50) ? "<option selected>" + limit + "</option>" : "" %>
        </select>条</label>
    </span>
    <span>
    <% if (offset == 0) {%>
    <a>|<-</a>
    <a><-</a>
    <% } else { %>
    <a href="javascript:page.first();">|<-</a>
    <a href="javascript:page.prev();"><-</a>
    <% } %>
    <% if (offset + limit >= total) {%>
    <a>-></a>
    <a>->|</a>
    <%} else {%>
    <a href="javascript:page.next();">-></a>
    <a href="javascript:page.last();">->|</a>
    <%}%>
    </span>
    <span>
        <label>跳转到第<select onchange="page.goto(this.value);">
            <% for (int i = 1; i <= pages; i++) { %>
            <option <%=(i == p) ? "selected" : ""%> value=<%=i%>><%=i%> / <%=pages%></option>
            <% } %>
        </select>页</label>
    </span>
</div>